#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QGraphicsView>
#include <QPainter>
#include <QFont>
#include <QPushButton>
#include <QDebug>
#include <QRect>
#include <QLabel>
#include <QTimer>
#include <QSettings>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow),
    help_scene(null),
    shop_scene(null),
    game_scene(null)

{
    ui->setupUi(this);

    load_save();

    mainscene();
    helpscene();
    shopscene();
    gamescene();

}

void MainWindow::mainscene(){
    //设置主界面属性
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    setWindowIcon(QIcon(":/resource/img/logo.png"));
    setWindowTitle("ShapeZ");

    imageLabel= new QLabel(this);
    QPixmap image(":/resource/img/logo.png");
    imageLabel->setPixmap(image);
    int imageWidth = image.width();
    int imageHeight = image.height();
    int x = (this->width() - imageWidth) / 2;
    int y = (this->height() - imageHeight) / 3;
    imageLabel->setGeometry(x, y, imageWidth, imageHeight);
}

void MainWindow::gamescene(){
    int btW = BUTTON_WIDTH, btH = BUTTON_HEIGHT;
    int Xpos = (this->width()-BUTTON_NUMBER*btW) / 2;

    QPushButton * newgameBtn = new QPushButton("新的游戏",this);
    newgameBtn->resize(btW,btH);
    newgameBtn->move(Xpos,this->height()-3*btH);

    QPushButton * continueBtn = new QPushButton("继续游戏",this);
    continueBtn->resize(btW,btH);
    continueBtn->move(Xpos + btW,this->height()-3*btH);

    connect(newgameBtn,&QPushButton::clicked,this,[=](){
        if(game_scene){delete game_scene;}
        data_manager.map_generator->generateMap();
        game_scene = new Game_Scene(&data_manager);
        game_scene->setGeometry(this->geometry());
        this->hide();
        game_scene->enterScene();
        connect(game_scene,&Game_Scene::chooseSceneBack,this,[=]()
        {
            this->setGeometry(game_scene->geometry());
            game_scene->leaveScene();
            this->show();
        });
        connect(game_scene, &Game_Scene::save,this,[=]{do_save();});
    });
    connect(continueBtn,&QPushButton::clicked,this,[=](){
        if(game_scene){delete game_scene;}
        game_scene = new Game_Scene(&data_manager);
        game_scene->setGeometry(this->geometry());
        this->hide();
        game_scene->enterScene();
        connect(game_scene,&Game_Scene::chooseSceneBack,this,[=]()
        {
            this->setGeometry(game_scene->geometry());
            game_scene->leaveScene();
            this->show();
        });
        connect(game_scene, &Game_Scene::save,this,[=]{do_save();});
    });
}

void MainWindow::shopscene(){
    QPushButton * shopBtn = new QPushButton("全局强化",this);
    shopBtn->resize(BUTTON_WIDTH,BUTTON_HEIGHT);
    shopBtn->move((this->width()-BUTTON_NUMBER*BUTTON_WIDTH) / 2 + 2*BUTTON_WIDTH,this->height()-3*BUTTON_HEIGHT);
    connect(shopBtn,&QPushButton::clicked,this,[=]()
    {
        if(!shop_scene)shop_scene = new Shop_Scene(&data_manager);
        shop_scene->setGeometry(this->geometry());
        this->hide();
        shop_scene->show();
        connect(shop_scene,&Shop_Scene::chooseSceneBack,this,[=]()
        {
            this->setGeometry(shop_scene->geometry());
            shop_scene->hide();
            this->show();
        });// 回归
        connect(shop_scene,&Shop_Scene::save,this,[=](){do_save();}); //关闭
    });
}

void MainWindow::helpscene(){
    QPushButton * helpBtn = new QPushButton("帮助界面",this);
    helpBtn->resize(150,150*0.618);
    helpBtn->move(this->width()-helpBtn->width(),this->height()-helpBtn->height());
    connect(helpBtn,&QPushButton::clicked,this,[=]()
    {
        if(!help_scene)help_scene = new Help_Scene;
        help_scene->setGeometry(this->geometry());
        this->hide();
        help_scene->show();
        connect(help_scene,&Help_Scene::chooseSceneBack,this,[=]()
        {
            this->setGeometry(help_scene->geometry());
            help_scene->hide();
            this->show();
        });// 回归
        connect(help_scene,&Help_Scene::save,this,[=](){do_save();});// 关闭
    });
}

void MainWindow::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/img/map.png");
    painter.drawPixmap(0,0,GAME_WIDTH,GAME_HEIGHT,pix);
}

void MainWindow::closeEvent(QCloseEvent *e){
    do_save();
    QMainWindow::closeEvent(e);
}

void MainWindow::do_save(){
    // // qDebug() << "Save successed!";
    if(game_scene){
        data_manager.has_game = true;
        game_scene->do_save();
    }
    else{
        data_manager.has_game = false;
    }
    data_manager.doSave();
}

void MainWindow::load_save(){
    data_manager.loadSave();
}

MainWindow::~MainWindow()
{
    delete ui;
    if(game_scene) delete game_scene;
    game_scene = null;
    if(shop_scene) delete shop_scene;
    shop_scene = null;
    if(help_scene) delete help_scene;
    help_scene = null;
}
